package com.ymatou.iapi.login.testcase;

import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.login.parameter.*;
import com.ymatou.iapi.login.service.*;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.data.LoginSafeConfiguration;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.JUserSafe;
//import com.ymttest.database.mongo.MongoLoginHistoryWapper;
//import com.ymttest.database.mongo.ULC201406Bean;
//import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.mongo.YmatouBlacklistUserWapper;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.DesUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * PC端IP区域认证接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_IpAreaAuthStatusRequestDto {
	private static IpAreaAuthStatusRequestDtoBean ipareaauthstatusrequestdtoBean;
	private static IpAreaAuthStatusRequestDtoCall ipareaauthstatusrequestdtoCall;
	// private static ULC201406Wapper userWapper;
	private static YmatouBlacklistUserWapper blackLisWapper;
	private static UserSafeWapper authWapper;
	private static final String SOURCETYPE = "mainsite";
	// private static MongoLoginHistoryWapper mloginHistoryWapper;
	private static final boolean DELETELOGINHISTORY = true;

	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("PC端IP区域认证接口");

	}

	@Before
	public void caseUp() throws Exception {
		config();
		ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
		ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
		// userWapper = new ULC201406Wapper();
		blackLisWapper = new YmatouBlacklistUserWapper();
		authWapper = new UserSafeWapper();
		// mloginHistoryWapper = new MongoLoginHistoryWapper();
	}

	@AfterClass
	public static void tearDown() throws Throwable {
		Logger.generateResult(ipareaauthstatusrequestdtoCall.getOpurl());
		// userWapper.close();
		blackLisWapper.close();
		authWapper.close();
		// mloginHistoryWapper.close();
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void config() throws InterruptedException {
		UserSafeWapper userSafeWapper = new UserSafeWapper();
		// 默认风控开关 dimension_strategy:checkOtherUserTypeLoginAuth=true
		userSafeWapper.updateRiskStrategyStatus(2, "DefaultSafeVerifyStrategy");
		// 买家设备号黑名单 dimension_strategy:deviceIdAllowNull=true
		userSafeWapper.updateRiskStrategyStatus(2, "BuyerDeviceIdBlacklistVerifyStrategy");
		// 买家ip区域认证(买家是否在此区域认证过)
		userSafeWapper.updateRiskStrategyStatus(2, "BuyerIpAreaVerifyStrategy");
		userSafeWapper.updateRiskStrategyStatus(2, "BuyerTokenVerifyStrategy");
		// 登入频次加入黑名单
		userSafeWapper.updateRiskStrategyStatus(0, "BuyerLoginCountVerifyStrategy");
		userSafeWapper.updateRiskStrategyStatus(0, "SellerLoginCountVerifyStrateg");		
	}

	@Test
	@Category(P1.class)
	@TestCase
	// 第一次登入如果是已经认证过的，现在成功
	public void Tc_IpAreaAuthStatusRequestDto_001() {
		Logger.start(true, "IP第一次登陆后再次认证成功");
		try {
			Logger.comment("前置条件准备");
			String ip = "116.228.41." + (new Random().nextInt(254) + 1);// china,
																		// shanghai,
																		// shanghai
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setClientIp(ip);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginId = urb.getLoginId();
			String deviceId = urb.getDeviceId();

			Logger.comment("如果此IP已认证过，先进行删除");
			authWapper.deleteIPAreaData(userId, "China", "Shanghai", "Shanghai");

			Logger.comment("IP区域登陆，当前设备认证状态是:" + LoginSafeConfiguration.AUTH_PC_OPEN + "(打开则会登陆失败，关闭则登陆成功)");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatus(lac);

			Logger.comment("登陆");
			LoginAuthCall lac2 = new LoginAuthCall();
			lac2.setData(lab);
			lac2.callService();

			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoBean.setClientPort("1234");
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);

			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");

			Thread.sleep(1000);
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Shanghai", "Shanghai");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_IpAreaAuthStatusRequestDto_002() {
		Logger.start(true, "使用Token失效的IP登陆，再次认证成功");
		try {
			Logger.comment("前置条件准备");
			String ip = "116.228.41." + (new Random().nextInt(254) + 1);// china,
																		// shanghai,
																		// shanghai
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setClientIp(ip);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginId = urb.getLoginId();
			String deviceId = urb.getDeviceId();
			checkIpLoginHistory(ip, YMTDateUtil.getDateForMongoFormat(-1, 0, 0));
			Logger.comment("修改token最后认证时间为100天前");
			authWapper.updateTokenUpdateTime(userId, null, null, null, YMTDateUtil.getBeforeOrNextDay(-100));

			Logger.comment("登陆");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatus(lac);

			Logger.comment("认证失败");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			authWapper.updateTokenUpdateTime(userId, null, null, null, YMTDateUtil.getBeforeOrNextDay(-10));

			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");

			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, null, null, null);
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_IpAreaAuthStatusRequestDto_003() {
		Logger.start(true, "黑名单IP第一次认证失败后，再次认证成功");
		try {
			Logger.debug("注册用户");
			List<Integer> maps = new ArrayList<>();
			String ip = TestDataManager.getRandomIp();
			for (int i = 0; i < 11; i++) {
				UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
				UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
				maps.add(uc.getUserid());
			}
			Thread.sleep(3000);
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			Logger.debug("登入用户触发ip黑名单");
			for (Integer userId : maps) {
				String loginid = new JUserWapper().selectUserinfoByUid(userId).get(0).getLoginId();
				lab = new LoginAuthBean();
				lac = new LoginAuthCall();
				lab.setLoginId(loginid);
				lab.setPas("1234561");
				lab.setClientIp(ip);
				lab.setSourceType("Mainsite");
				lac.setData(lab);
				lac.callService();
			}
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Logger.debug("测试用户触发ip黑名单");
			int userId = uc.getUserid();
			String loginid = new JUserWapper().selectUserinfoByUid(userId).get(0).getLoginId();
			lab = new LoginAuthBean();
			lac = new LoginAuthCall();
			lab.setLoginId(loginid);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lab.setClientIp(ip);
			lab.setSourceType("Mainsite");
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals(false, lac.getSuccess(), "验证Success");

			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(urb.getLoginId());
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(false);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			LoginSafeConfiguration.checkAuthStatus(false, ipareaauthstatusrequestdtoCall);

			lab = new LoginAuthBean();
			lac = new LoginAuthCall();
			lab.setLoginId(loginid);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lab.setClientIp(ip);
			lab.setSourceType("Mainsite");
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals(false, lac.getSuccess(), "验证Success");

			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(urb.getLoginId());
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);

			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, null, null, null);
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_IpAreaAuthStatusRequestDto_004() {
		Logger.start(true, "按配置类型判断是否让用户执行安全流程:www.ymatou.com,mainsite,app,check_usertype_0");
		try {
			List<Integer> maps = new ArrayList<>();
			String ip = TestDataManager.getRandomIp();
			for (int i = 0; i < 11; i++) {
				UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
//				urb.setTokenSource("app");
				UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
				maps.add(uc.getUserid());
			}
			Thread.sleep(3000);
			Logger.comment("添加IP黑名单");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			for (Integer userId : maps) {
				String loginid = new JUserWapper().selectUserinfoByUid(userId).get(0).getLoginId();
				lab = new LoginAuthBean();
				lac = new LoginAuthCall();
				lab.setLoginId(loginid);
//				lab.setTokenSource("app");
				lab.setPas("1234561");
				lab.setClientIp(ip);
				lab.setSourceType("Mainsite");
				lac.setData(lab);
				lac.callService();
			}
			String email = TestDataManager.getEmail();
			String phone = TestDataManager.getPhoneNum();
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
//			urb.setTokenSource("app");
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginid = new JUserWapper().selectUserinfoByUid(userId).get(0).getLoginId();

			Logger.comment("登陆");
			lab = new LoginAuthBean();
			lac = new LoginAuthCall();
			lab.setLoginId(loginid);
			lab.setClientIp(ip);
			lab.setSourceType("mainsite");
//			lab.setTokenSource("app");
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatusWithBlackIp(lac, false, null);

			Logger.comment("认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginid);
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
//			ipareaauthstatusrequestdtoBean.setTokenSource("app");
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	// 第一次登入如果是已经认证过的，现在成功
	public void Tc_IpAreaAuthStatusRequestDto_005() {
		Logger.start(true, "传入TokenSource为app用户注册成功JIRACONN-2123");
		try {
			Logger.comment("前置条件准备");
			// int n=new Random().nextInt(20)+200;
			String ip = "180.152."+ (new Random().nextInt(254) + 1)+"." + (new Random().nextInt(254) + 1);// china,
																		// shanghai,
																		// shanghai
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
//			urb.setClientIp(ip);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginId = urb.getLoginId();

			Logger.comment("如果此IP已认证过，先进行删除");
			authWapper.deleteIPAreaData(userId, "China", "Shanghai", "Shanghai");

			Logger.comment("IP区域登陆，当前设备认证状态是:" + LoginSafeConfiguration.AUTH_PC_OPEN + "(打开则会登陆失败，关闭则登陆成功)");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType("app"); 
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			Logger.verifyEquals("false", lac.getString("Success"), "验证登陆失败");


			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoBean.setTokenSource("app");
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);
			String Sign = ipareaauthstatusrequestdtoCall.getJsonObject().get("Result").getAsJsonObject().get("UserData").getAsJsonObject().get("Sign").getAsString();
			// 长度不校验
			// Logger.verifyEquals(128, Sign.length(),
			// "TokenSource验证Sign长度为128");
			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lab.setTokenSource("app");
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");

			Thread.sleep(1000);
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Shanghai", "Shanghai");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	// 第一次登入如果是已经认证过的，现在成功
	public void Tc_IpAreaAuthStatusRequestDto_006() {
		Logger.start(true, "双重认证开启IP认证成功JIRACONN-2522");
		try {
			Logger.comment("前置条件准备");
			String ip = "180.152."+ (new Random().nextInt(254) + 1)+"." + (new Random().nextInt(254) + 1);// china,
																		// shanghai,
																		// shanghai
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setClientIp(ip);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginId = urb.getLoginId();
			String deviceId = urb.getDeviceId();
			Thread.sleep(2000);
			Logger.comment("如果此IP已认证过，先进行删除");
			authWapper.deleteIPAreaData(userId, "China", "Shanghai", "Shanghai");

			AuthCallServiceV2.setSecurityQuestion(userId);
			AuthCallServiceV2.setSecurity(userId, true);

			Logger.comment("IP区域登陆，当前设备认证状态是:" + LoginSafeConfiguration.AUTH_PC_OPEN + "(打开则会登陆失败，关闭则登陆成功)");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatus(lac);

			Logger.comment("登陆");
			LoginAuthCall lac2 = new LoginAuthCall();
			lac2.setData(lab);
			lac2.callService();

			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoBean.setVerifySecurityQuestion(true);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();

			Thread.sleep(1000);

			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");

			Thread.sleep(1000);
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Shanghai", "Shanghai");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	// 第一次登入如果是已经认证过的，现在成功
	public void Tc_IpAreaAuthStatusRequestDto_007() {
		Logger.start(true, "双重认证开启VerifySecurityQuestion未传IP认证失败JIRACONN-2522");
		try {
			Logger.comment("前置条件准备");
			String ip = "180.152."+ (new Random().nextInt(254) + 1)+"." + (new Random().nextInt(254) + 1);// china,
																		// shanghai,
																		// shanghai
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setClientIp(ip);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginId = urb.getLoginId();
			String deviceId = urb.getDeviceId();
			Thread.sleep(2000);
			Logger.comment("如果此IP已认证过，先进行删除");
			authWapper.deleteIPAreaData(userId, "China", "Shanghai", "Shanghai");

			AuthCallServiceV2.setSecurityQuestion(userId);
			AuthCallServiceV2.setSecurity(userId, true);

			Logger.comment("IP区域登陆，当前设备认证状态是:" + LoginSafeConfiguration.AUTH_PC_OPEN + "(打开则会登陆失败，关闭则登陆成功)");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatus(lac);

			Logger.comment("登陆");
			LoginAuthCall lac2 = new LoginAuthCall();
			lac2.setData(lab);
			lac2.callService();

			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();

			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("false", lac3.getString("Success"), "验证登陆成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_IpAreaAuthStatusRequestDto_008() {
		Logger.start(true, "双重认证开启VerifySecurityQuestion传falseIP认证失败JIRACONN-2522");
		try {
			Logger.comment("前置条件准备");
			String ip = "180.152."+ (new Random().nextInt(254) + 1)+"." + (new Random().nextInt(254) + 1);// china,
																		// shanghai,
																		// shanghai
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setClientIp(ip);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginId = urb.getLoginId();
			String deviceId = urb.getDeviceId();
			Thread.sleep(2000);
			Logger.comment("如果此IP已认证过，先进行删除");
			authWapper.deleteIPAreaData(userId, "China", "Shanghai", "Shanghai");

			AuthCallServiceV2.setSecurityQuestion(userId);
			AuthCallServiceV2.setSecurity(userId, true);

			Logger.comment("IP区域登陆，当前设备认证状态是:" + LoginSafeConfiguration.AUTH_PC_OPEN + "(打开则会登陆失败，关闭则登陆成功)");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			LoginSafeConfiguration.checkLoginStatus(lac);

			Logger.comment("登陆");
			LoginAuthCall lac2 = new LoginAuthCall();
			lac2.setData(lab);
			lac2.callService();

			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPassword(JUserCallService.defaultLoginPwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoBean.setVerifySecurityQuestion(false);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();

			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("false", lac3.getString("Success"), "验证登陆成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	// 第一次登入如果是已经认证过的，现在成功
	public void Tc_IpAreaAuthStatusRequestDto_009() {
		Logger.start(true, "IP第一次登陆后再次认证成功,使用加密密码");
		try {
			Thread.sleep(2000);
			Logger.comment("前置条件准备");
			String ip = "116.228.42." + (new Random().nextInt(254) + 1);// china,
																		// shanghai,
																		// shanghai
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setClientIp(ip);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginId = urb.getLoginId();
			String deviceId = urb.getDeviceId();

			Logger.comment("如果此IP已认证过，先进行删除");
			authWapper.deleteIPAreaData(userId, "China", "Shanghai", "Shanghai");
			Logger.comment("IP区域登陆，当前设备认证状态是:" + LoginSafeConfiguration.AUTH_PC_OPEN + "(打开则会登陆失败，关闭则登陆成功)");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			if(lac.getString("Success").equals("true")){
				Thread.sleep(1000);
				Logger.comment("再次登陆");
				LoginAuthCall lac2 = new LoginAuthCall();
				lac2.setData(lab);
				lac2.callService();
				Logger.verifyEquals("false", lac.getString("Success"), "此时应该被风控");
			}

			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPasswordType(1);
			String pwd = DesUtil.encrypt(JUserCallService.defaultLoginPwd,DesUtil.DES_KEY_STRING,DesUtil.DES_IVPARAMETER);
			ipareaauthstatusrequestdtoBean.setPassword(pwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);

			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");

			Thread.sleep(1000);
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Shanghai", "Shanghai");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	// 第一次登入如果是已经认证过的，现在成功
	public void Tc_IpAreaAuthStatusRequestDto_010() {
		Logger.start(true, "IP第一次登陆后再次认证成功,使用加密密码2");
		try {
			Logger.comment("前置条件准备");
			Thread.sleep(2000);
			String ip = "116.228.43." + (new Random().nextInt(254) + 1);// china,
																		// shanghai,
																		// shanghai
			UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
			urb.setClientIp(ip);
			UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
			Integer userId = uc.getUserid();
			String loginId = urb.getLoginId();
			String deviceId = urb.getDeviceId();

			Logger.comment("如果此IP已认证过，先进行删除");
			authWapper.deleteIPAreaData(userId, "China", "Shanghai", "Shanghai");
			Logger.comment("IP区域登陆，当前设备认证状态是:" + LoginSafeConfiguration.AUTH_PC_OPEN + "(打开则会登陆失败，关闭则登陆成功)");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(loginId);
			lab.setClientIp(ip);
			lab.setSourceType(SOURCETYPE);
			lab.setPas(JUserCallService.defaultLoginPwd);
			lac.setData(lab);
			lac.callService();
			if(lac.getString("Success").equals("true")){
				Thread.sleep(1000);
				Logger.comment("再次登陆");
				LoginAuthCall lac2 = new LoginAuthCall();
				lac2.setData(lab);
				lac2.callService();
				Logger.verifyEquals("false", lac.getString("Success"), "此时应该被风控");
			}

			Logger.comment("开始第二次认证成功");
			ipareaauthstatusrequestdtoBean = new IpAreaAuthStatusRequestDtoBean();
			ipareaauthstatusrequestdtoCall = new IpAreaAuthStatusRequestDtoCall();
			ipareaauthstatusrequestdtoBean.setIp(ip);
			ipareaauthstatusrequestdtoBean.setLoginId(loginId);
			ipareaauthstatusrequestdtoBean.setPasswordType(2);
			 byte[] rsabytes = RSAUtils.encryptByPublicKey(urb.getPas().getBytes(),
						(RSAPublicKey) RSAUtils.loadPublicKey(EnvSetup
								.getData("USER_PUBLIC_KEY")));
				String rsanpwd = Base64.getEncoder().encodeToString(rsabytes);
			ipareaauthstatusrequestdtoBean.setPassword(rsanpwd);
			ipareaauthstatusrequestdtoBean.setSuccess(true);
			ipareaauthstatusrequestdtoBean.setSource(SOURCETYPE);
			ipareaauthstatusrequestdtoCall.setData(ipareaauthstatusrequestdtoBean);
			ipareaauthstatusrequestdtoCall.callService();
			LoginSafeConfiguration.checkAuthStatus(true, ipareaauthstatusrequestdtoCall);

			Logger.comment("登陆");
			LoginAuthCall lac3 = new LoginAuthCall();
			lac3.setData(lab);
			lac3.callService();
			Logger.verifyEquals("true", lac3.getString("Success"), "验证登陆成功");

			Thread.sleep(1000);
			Logger.comment("验证认证数据已写进数据库");
			Map<String, Object> ipAuth2 = authWapper.getIPAuthByUserIdAndArea(userId, "China", "Shanghai", "Shanghai");
			Logger.verifyEquals(200, ipAuth2.get("AuthStatus"), "验证认证信息成功");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	public void checkIpLoginHistory(String ip, String dateTime) throws Exception {
		if (DELETELOGINHISTORY) {
			blackLisWapper.deleteData(ip);
		}
	}
}